package cn.com.wdjedu.androidapp.wjeduandroid.ui.baseui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * User: qii
 * Date: 12-12-30
 * 能够获取当前状态
 */
public class AbstractAppFragment extends FixedOnActivityResultBugFragment {
    /**
     * when activity is recycled by system, isFirstTimeStartFlag will be reset to default true,
     * when activity is recreated because a configuration change for example screen rotate,
     * isFirstTimeStartFlag will stay false
     */
    private boolean isFirstTimeStartFlag = true;

    protected final int FIRST_TIME_START = 0; //when activity is first time start
    protected final int SCREEN_ROTATE = 1;
    //when activity is destroyed and recreated because a configuration change, see setRetainInstance(boolean retain)
    protected final int ACTIVITY_DESTROY_AND_CREATE = 2;
    //when activity is destroyed because memory is too low, recycled by android system

    private final int mLoginRequestCode = 0x100;
    public static final String EXTRA_LOGIN_INFO = "loginInfo";


    protected int getCurrentState(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            isFirstTimeStartFlag = false;
            return ACTIVITY_DESTROY_AND_CREATE;
        }

        if (!isFirstTimeStartFlag) {
            return SCREEN_ROTATE;
        }

        isFirstTimeStartFlag = false;
        return FIRST_TIME_START;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在配置变化的时候将这个fragment保存下来
        setRetainInstance(true);
    }


    private final String mSaveStateKey = "internalSavedViewState08512";
    private Bundle saveState; /*状态保存bundle*/

    /**
     * 是否恢复保存的状态
     */
    private boolean restoreStateFromArguments() {
        Bundle args = getArguments();
        if (args == null) { /*首次加载*/
            return false;
        }
        saveState = args.getBundle(mSaveStateKey);
        if (saveState != null) {
            onRestoreState(saveState);
            return true;
        }
        return false;
    }

    /**
     * 恢复fragment状态
     */
    public void onRestoreState(@NonNull Bundle saveState) {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 恢复状态
        if (!restoreStateFromArguments()) {
            onFirstTimeLaunched(); /*首次加载数据*/
        }
    }

    /**
     * 首次加载数据
     */
    public void onFirstTimeLaunched() {
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveStateToArguments(); /*保存恢复数据*/
    }

    /**
     * 保存数据到Arguments
     */
    private void saveStateToArguments() {
        Bundle args;
        if ((args = getArguments()) != null) {
            saveState = args.getBundle(mSaveStateKey);
        }
        saveState = saveState == null ? (new Bundle()) : saveState;  /*初始化saveState*/

        if (getView() != null) { /*添加保存数据*/
            onSaveState(saveState);
        }
        if (args != null) {
            args.putBundle(mSaveStateKey, saveState);
        }
    }

    /**
     * 保存恢复数据
     */
    public void onSaveState(@NonNull Bundle outState) {
    }
}
